import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";
import {userMenu} from "@/api/user";
import {listToTree} from "@/utils/treeUtil";


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/LoginView.vue')
    },
    {
        path: '/index',
        name: 'index',
        component: () => import('@/views/layout/IndexView.vue'),
        children: [
            // {
            //     path: '/home',
            //     name: 'home',
            //     component: () => import('@/views/home/HomeView.vue')
            // },
            // {
            //     path: '/sys/user',
            //     name: 'user',
            //     component: () => import('@/views/sys/user/UserView.vue')
            // },
            // {
            //     path: '/sys/role',
            //     name: 'role',
            //     component: () => import('@/views/sys/role/RoleView.vue')
            // },
            // {
            //     path: '/sys/authority',
            //     name: 'auth',
            //     component: () => import('@/views/sys/auth/AuthView.vue')
            // },
            // {
            //     path: '/sys/dept',
            //     name: 'dept',
            //     component: () => import('@/views/sys/dept/DeptView.vue')
            // },
            // {
            //     path: '/**',
            //     name: 'page404',
            //     component: () => import('@/views/page404/Page404View.vue')
            // }
        ]
    }
]

const router = new VueRouter({
    routes
})


/*
* 配置路由守卫
*  to: Route: 即将要进入的目标 路由对象
*  from: Route: 当前导航正要离开的路由
*  next(): 进行管道中的下一个钩子  放行路由
* */
router.beforeEach(async (to, from, next) => {
    // 判定用户是否登录
    let token = sessionStorage.getItem("token");
    // 没有认证  访问的不是login页面
    if (!token && to.path != '/login') {
        next("/login")
    }
    //认证通过  访问login页面
    if (token && to.path == '/login') {
        next('/index')
    }

    // 判定用户是否登录成功
    if (token) {
        // 获取用户的菜单权限  判定用户是否已经拥有了菜单权限数据
        if (store.state.menuList.length == 0) {
            // 发送请求 获取用户的菜单权限
            let {data} =  await userMenu()
            let dbMenuTree = listToTree(data.menuList)   // [{},{}]
            let lastMenu = [
                {
                    path: '/home',
                    title: '首页',
                    icon: 'el-icon-s-home'
                },
                ...dbMenuTree
            ]
            // 将用户的菜单信息存储 vuex
            store.commit('initMenu',lastMenu)
            // 设置动态路由  菜单权限
            let dbRouter =   data.menuList.filter(m=>m.isMenu==1)
                .map(m=>{
                    return{
                        path: m.path,   //  /sys/user       @/views/sys/user/UserView
                        component: () => import('@/views'+m.path+'/'+m.component)
                    }
                })

            let  lastRouter = [
                {
                    path: '/home',
                    component: () => import('@/views/home/HomeView.vue')
                },
                ...dbRouter,
                {
                    path: '/**',
                    component: () => import('@/views/page404/Page404View.vue')
                }
            ]
            // 遍历路由
            lastRouter.forEach(rt=>{
                router.addRoute('index',rt)
            })
            // 重定向路由
            next(to.path)
        }
    }
    next()
})

export default router
